package com.ss.test09;

/**
 * @Auther: 520ljs
 * @Date: 2022/3/19 - 03 - 19 - 22:23
 */
public class BuyTicketThread implements Runnable {
    int ticketNum = 10;

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            synchronized (this) {//把具有安全隐患的代码锁住即可，如果锁多了就会效率低,this就是锁
                if (ticketNum > 0) {
                    System.out.println("我在" + Thread.currentThread().getName() + "买到了背景到哈尔滨的第" + ticketNum-- + "张车票");
                }
            }
        }
    }
}
